package com.jotton.sampleV16.Fragments;



import java.util.LinkedList;
import java.util.List;

import com.jotton.sampleV16.R;
import com.jotton.sampleV16.R.id;

import android.app.Activity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class BookmarkFragment extends ListFragment {
	private static final String TAG = BookmarkFragment.class.getName();
	
	public static final String[][] BOOKMARKS = {
		{ "Back",	null },
		{ "Google",		"http://www.google.co.jp/" },
		{ "bing",		"http://www.bing.com/" },
		{ "Baidu",		"http://www.baidu.jp/" },
		{ "Naver",		"http://www.naver.jp/" },
		{ "Yahoo!",		"http://www.yahoo.co.jp/" },
		{ "goo",		"http://www.goo.ne.jp/" },
		{ "@nifty",		"http://www.nifty.com/" },
		{ "Biglobe",	"http://www.biglobe.ne.jp/" },
		{ "infoseek",	"http://www.infoseek.co.jp/" },
		{ "excite",		"http://www.excite.co.jp/" },
		{ "livedoor",	"http://www.livedoor.com/" },
		{ "MSN",		"http://jp.msn.com/" },
	};

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		Log.d(TAG, "onAttach");
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");

//		List<String> list = new LinkedList<String>();
//		for (String[] bookmark : BOOKMARKS) {
//			list.add(bookmark[0]);
//		}
//		
//		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
//		
//		ListView listView = new ListView(getActivity());
//		listView.setAdapter(adapter);
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onActivityCreated(Bundle bundle) {
		super.onActivityCreated(bundle);
		Log.d(TAG, "onActivityCreated");
		List<String> list = new LinkedList<String>();
		for (String[] bookmark : BOOKMARKS) {
			list.add(bookmark[0]);
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
		
		setListAdapter(adapter);
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.d(TAG, "onStart: visible=" + isVisible());
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume: visible=" + isVisible());
	}

	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause: visible=" + isVisible());
	}

	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "onStop: visible=" + isVisible());
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.d(TAG, "onDestroyView");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}

	@Override
	public void onDetach() {
		super.onDetach();
		Log.d(TAG, "onDetach");
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Log.d(TAG, "onListItemClick");
		if (position == 0) {
			getFragmentManager().popBackStack();
		} else {
			String url = BOOKMARKS[position][1];
			WebActivity web = new WebActivity(url);
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			ft.replace(R.id.fragment2, web);
			ft.addToBackStack(null);
			ft.commit();
		}
	}
}
